﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Configuration;

namespace MobileIdeas.MobileTasks.SDK.DAL.DataAccess
{
    public abstract class DataAccessObject: IDisposable
    {
        private MobiletasksEntities entities;
        private bool isDisposed;
        private EntityConnection connection;

        protected MobiletasksEntities EntityContext
        {
            get
            {
                if (this.isDisposed)
                {
                    throw new ObjectDisposedException("The data access object has been disposed.");
                }

                return this.connection == null
                           ? new MobiletasksEntities()
                           : new MobiletasksEntities(this.connection);
            }
        }

        public void Dispose()
        {
            this.Dispose(true);

            GC.SuppressFinalize(this);
        }


        protected virtual void Dispose(bool disposing)
        {
            if (this.connection != null)
            {
                this.connection.Dispose();
                this.connection = null;
            }

            this.isDisposed = true;
        }

        public DataAccessObject()
        {
            try
            {
                this.connection = new EntityConnection(ConnectionManager.ConnectionString);
                this.connection.Open();
            }
            catch (Exception ex)
            {
                //TODO: manejo de excepciones
            }
        }


    }
}
